package com.zeldu.uaa.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zeldu.common.exception.CustomException;
import com.zeldu.common.model.response.CommonCode;
import com.zeldu.common.util.PhoneUtil;
import com.zeldu.uaa.entity.Student;
import com.zeldu.uaa.entity.Teacher;
import com.zeldu.uaa.mapper.StudentMapper;
import com.zeldu.uaa.mapper.TeacherMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.support.RequestContext;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @version 1.0
 **/
@Service
public class SpringDataUserDetailsService implements UserDetailsService {
    @Autowired
    TeacherMapper teacherMapper;
    @Autowired
    StudentMapper studentMapper;



    //根据 账号查询用户信息
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //RequestContext requestContext=new RequestContext();

     //   Boolean matches = PhoneUtil.isMatches(phoneNum);
         HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
         String identity = request.getHeader("identity");
       // if(!matches) return null;
         if("student".equals(identity)){
             Student student = studentMapper.findByName(username);
             if(student==null) return null;

             //将来连接数据库根据账号查询用户信息
             String permission=student.getPermission();
             //根据用户的id查询用户的权限
             List<String> permissions = new ArrayList<>();
             permissions.add(permission);
             //将permissions转成数组
             String[] permissionArray = new String[permissions.size()];
             permissions.toArray(permissionArray);
             //将userDto转成json
             String password=student.getPassWord();
             student.setPassWord(null);

             String principal = JSON.toJSONString(student);

             UserDetails userDetails = User.withUsername(principal).password(password).authorities(permissionArray).build();
             return userDetails;
         }else if("teacher".equals(identity)){
              Teacher teacher = teacherMapper.findByName(username);
             if(teacher==null) return null;

             //将来连接数据库根据账号查询用户信息
             String permission=teacher.getPermission();
             //根据用户的id查询用户的权限
             List<String> permissions = new ArrayList<>();
             permissions.add(permission);
             //将permissions转成数组
             String[] permissionArray = new String[permissions.size()];
             permissions.toArray(permissionArray);
             //将userDto转成json
             String password=teacher.getPassWord();
             teacher.setPassWord(null);

             String principal = JSON.toJSONString(teacher);

             UserDetails userDetails = User.withUsername(principal).password(password).authorities(permissionArray).build();
             return userDetails;
         }
        return null;
       //  throw  new CustomException(CommonCode.Wrong_Identity);


    }
}
